package com.LC.DAO;

import com.LC.Entity.Permission;
import com.LC.Entity.Role;
import com.LC.Entity.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

@Mapper
public interface UserDAO {
    
    //根据用户名查询用户数据
    @Select("select * from t_user where username = #{username}")
    public User findByUsername(String username);
    
    //根据用户id查询用户角色
    @Select("SELECT * FROM t_role tr,t_user_role tur WHERE tr.id = tur.role_id  AND tur.user_id = #{userId}")
    public Set<Role> RoleSetByUserId(int userId);
    
    //根据角色id查询权限
    @Select("select * from t_permission tp, t_role_permission trp where tp.id = trp.permission_id and trp.role_id = #{roleId}")
    public Set<Permission> PermissionSetByRoleId(int roleId);

    @Select("<script>"+
      "select * from t_user" +
      "<if test = 'value != null and value.length > 0'>" +
      "where telephone = #{value} or username = #{value}"+
      "</if>" +
      "</script>")
    public Page<User> userPage(String queryString);
    
    @Insert("insert into t_user values(null,#{birthday},#{gender},#{username},#{password},#{remark},#{station},#{telephone})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void AddUser(User addUser);
    
    @Insert("insert into t_user_role values(#{userId},#{roleId})")
    public void addTot_user_role(@Param("userId") int userId, @Param("roleId") int roleId);
    
    @Select("select * from t_user where id = #{id}")
    public User findUserById(int id);
    
    @Select("select role_id from t_user_role where user_id = #{id}")
    public List<Integer> RoleNumByUId(int id);
    
    @Update("update t_user set birthday = #{birthday}, gender = #{gender},username = #{username},password = #{password}," +
      "remark = #{remark},station = #{station},telephone = #{telephone} where id = #{id}")
    public void updateUser(User user);
    
    @Delete("delete from t_user_role where user_id = #{uid}")
    public void deleteFrom_t_user_role(int uid);
    
    @Delete("delete from t_user where id = #{id}")
    public void deleteUserById(int id);
    
 
}
